#include "messagebubble.h"
#include <QFormLayout>
#include <QImage>
#include <QLabel>
#include <QPushButton>
#include <QScrollArea>
#include <QVBoxLayout>
#include <QWidget>
#include "./ui_MessageBubble.h"

MessageBubble::MessageBubble(QWidget *parent,
                             QString icon,
                             QString name,
                             QString text,
                             MessageType messageType,
                             bool self,
                             bool showName)
    : QWidget(parent)
    , ui(new Ui::MessageBubble)
    , _showName(showName)
    , _self(self)
    , _text(text)
    , _messageType(messageType)
    , _icon(icon)
    , _name(name)

{
    ui->setupUi(this);
    QPixmap p(_icon);
    p = p.scaled(ui->icon_lb->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
    ui->icon_lb->setPixmap(p);
    ui->name_lb->setText(_name);
    if (!showName) {
        ui->name_lb->hide();
    }
    if (!_self) {
        ui->horizontalLayout->setDirection(QBoxLayout::LeftToRight);
        ui->horizontalLayout_2->setDirection(QBoxLayout::LeftToRight);
        QPixmap pixmap(":/icon/icon/tangle.png");
        pixmap = pixmap.scaled(ui->tangle->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
        ui->tangle->setPixmap(pixmap);
    } else {
        ui->horizontalLayout->setDirection(QBoxLayout::RightToLeft);
        ui->horizontalLayout_2->setDirection(QBoxLayout::RightToLeft);
        QPixmap pixmap(":/icon/icon/tangle.png");
        QImage image = pixmap.toImage();
        QImage flippedImage = image.mirrored(true, false);
        QPixmap flippedPixmap = QPixmap::fromImage(flippedImage);
        flippedPixmap = flippedPixmap.scaled(ui->tangle->size(),
                                             Qt::IgnoreAspectRatio,
                                             Qt::SmoothTransformation);
        ui->tangle->setPixmap(flippedPixmap);
    }
    ui->message->setWordWrap(true);
    setText(_text);
}

MessageBubble::~MessageBubble()
{
    delete ui;
}

void MessageBubble::setText(QString text)
{
    _text = text;
    QString t = adjustContentRectSize();
    ui->message->setText(t);
    ui->message->adjustSize();
    adjustSize();
}

void MessageBubble::paintEvent(QPaintEvent *event)
{
    QWidget::paintEvent(event);
}

QString MessageBubble::adjustContentRectSize()
{
    QFontMetricsF ft(ui->message->font());
    qreal currentRowLength = 0;
    qreal fontwidth = 0;
    int rowNum = 1;
    QString message;
    int max_width = width() * 0.7;
    for (int i = 0; i < _text.length(); ++i) {
        fontwidth = ft.horizontalAdvance(_text.at(i));
        if (currentRowLength + fontwidth > max_width) {
            currentRowLength = fontwidth;
            rowNum++;
            message.append("\n");
        } else {
            currentRowLength += fontwidth;
        }
        message.append(_text.at(i));
    }
    return message;
}

void MessageBubble::resizeEvent(QResizeEvent *event)
{
    QString t = adjustContentRectSize();
    ui->message->setText(t);
    ui->message->adjustSize();
}

QSize MessageBubble::sizeHint() const
{
    // 显示姓名
    if (_showName) {
        int w = width();
        int h = ui->name_lb->height() + ui->message->height() + contentsMargins().top()
                + contentsMargins().bottom() + 10;
        return QSize(w, h);
    } else {
        int w = width();
        int h = ui->message->height() + contentsMargins().top() + contentsMargins().bottom() + 10;
        return QSize(w, h);
    }
}
